package com.tanhua.recommend.msg;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.tanhua.dubbo.server.pojo.Publish;
import com.tanhua.recommend.pojo.RecommendQuanZi;
import com.tanhua.recommend.pojo.RecommendVideo;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.stereotype.Component;

/**
 * * @title: VideoMsgConsumer
 *
 * @Author Philcan
 * @Date: 2021/8/21 15:32
 * @Version 1.0
 */
@Component
@Slf4j
@RocketMQMessageListener(topic = "tanhua-video", consumerGroup = "tanhua-video-consumer")
public class VideoMsgConsumer implements RocketMQListener<String> {
    @Autowired
    private MongoTemplate mongoTemplate;


    @Override
    public void onMessage(String s) {
        try {
            JSONObject jsonObject = JSONUtil.parseObj(s);
            Long userId = jsonObject.getLong("userId");
            Long vid = jsonObject.getLong("vid");
            Integer type = jsonObject.getInt("type");

            RecommendVideo recommendVideo = new RecommendVideo();
            recommendVideo.setId(ObjectId.get());
            recommendVideo.setVideoId(vid);
            recommendVideo.setDate(System.currentTimeMillis());
            recommendVideo.setUserId(userId);

            switch (type) {
                case 1: {
                    recommendVideo.setScore(2d);
                    break;
                }
                case 2: {
                    recommendVideo.setScore(5d);
                    break;
                }
                case 3: {
                    recommendVideo.setScore(-5d);
                    break;
                }
                case 4: {
                    recommendVideo.setScore(10d);
                    break;
                }
                default: {
                    recommendVideo.setScore(0d);
                    break;
                }
            }

            this.mongoTemplate.save(recommendVideo);
        } catch (Exception e) {
            log.error("处理小视频粗错,msg=" + s, e);
        }

    }
}

